package com.base.commonlib.utils;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import com.base.commonlib.R;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.databinding.ItemDragVaildBinding;
import com.base.commonlib.widget.DragImageView;

/**
 * Created by weikailiang on 2020/3/12.
 */

public class DragImageUtils {
    private int i=10;
    public Handler handler;
    ItemDragVaildBinding mBinding;
    PopupWindow popupWindow;
    Handler mHandler;
    public Handler hand = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    i--;
                    if (mBinding!=null){
                        mBinding.time.setText(i+"s");
                    }
                    if (i==0){
                        if (popupWindow!=null){
                            popupWindow.dismiss();
                            hand.removeCallbacksAndMessages(null);
                            mHandler.sendEmptyMessage(4);
                        }
                    }else {
                        hand.sendEmptyMessageDelayed(1,1000);
                    }
                    break;
            }

        }
    };

    //图形验证弹窗
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void showDragImageVaild(final View v, final Activity activity, final Handler mHandler){
        //设置contentView
        this.mHandler = mHandler;
        final View contentView = LayoutInflater.from(v.getContext()).inflate(R.layout.item_drag_vaild, null);
        mBinding = DataBindingUtil.bind(contentView);
        popupWindow = new PopupWindow(contentView, BaseApp.SCREEN_WIDTH, BaseApp.SCREEN_HEIGHT);
        popupWindow.setContentView(contentView);
        //点窗口外关闭
//        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(activity,R.color.color_white));
        popupWindow.setFocusable(false);
        //全局弹出
//        popupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        popupWindow.setOutsideTouchable(false);

        hand.sendEmptyMessageDelayed(1,1000);

        //显示位置

        popupWindow.showAtLocation(v,Gravity.CENTER,0,0);

        mBinding.dragView.setUp(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_vaild),
                BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_vaild),
                BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_vaild),
                1f);

        // 设置需要改变颜色的部分
//        tvTest.setColorRegex("Yolanda", 0xfff75151);
//        tvTest.setText("android开发者论坛: Yolanda");
//        tvTest.setDiyTextImage("android开发者论坛: Yolanda 8833", "[\\d]+",
//                BitmapFactory.decodeResource(getResources(), R.drawable.drag_btn));
        mBinding.dragView.setDragListenner(new DragImageView.DragListenner() {
            @Override
            public void onDrag(float position) {
                if (Math.abs(position) < 1)
                    mBinding.dragView.fail();
                else {
                    mBinding.dragView.ok();
                    runUIDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mBinding.dragView.reset();
                            popupWindow.dismiss();
                            mHandler.sendEmptyMessageDelayed(1,1000);//重新定时
                            mHandler.sendEmptyMessageDelayed(2,1000);//重新定时
                            mHandler.sendEmptyMessageDelayed(3,1000);//重新定时
                            mHandler.sendEmptyMessage(4);
                        }
                    }, 2000);
                }
            }

        });

    }
    public void runUIDelayed(Runnable run, int de) {
        if (handler == null)
            handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(run, de);
    }
    public void showHid(boolean flag,Activity activity){
        if (flag) {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 0.5f;
            activity.getWindow().setAttributes(lp);
        }else {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 1.0f;
            activity.getWindow().setAttributes(lp);
        }
    }
    public void onDestory(){
        if (handler!=null){
            handler.removeCallbacksAndMessages(null);
        }
        if (hand!=null){
            hand.removeCallbacksAndMessages(null);
        }
    }
}
